<template>
	<view class="oneprice_page">
		<view class="topbar" :style="{'height':topHeight+'px'}"></view>
		<view class="page_title">
			<u-icon class="back_icon" name="arrow-left" size="24" color="#FFFFFF" @click="back()"></u-icon>
			<view>{{type==1?'一口价下单':type==2?'报价下单':type==3?'悬赏下单':'房屋工程下单'}}</view>
		</view>
		<view class="leimu">
			<view class="leimu_title">物品类目</view>
			<view>
				<u-input class="ipt" v-model="leimu" inputAlign="right" color="#999999" :disabled="true"
					placeholder-class="uinpt" placeholder="选择物品类目">
					<template slot="suffix">
						<u-icon @click="leimu_show=!leimu_show" name="arrow-right"></u-icon>
					</template>
				</u-input>
			</view>
		</view>
		<view class="leimu" style="margin-top: 20rpx;">
			<view class="leimu_title">服务项目</view>
			<view>
				<u-input class="ipt" v-model="fuwu" inputAlign="right" color="#999999" :disabled="true"
					placeholder-class="uinpt" placeholder="选择服务项目">
					<template slot="suffix">
						<u-icon @click="fuwu_show=!fuwu_show" name="arrow-right"></u-icon>
					</template>
				</u-input>
			</view>
		</view>
		<view class="leimu" style="margin-top: 20rpx;">
			<view class="leimu_title">配件详情</view>
			<view>
				<u-input class="ipt" v-model="peijian" inputAlign="right" color="#999999" :disabled="true"
					placeholder-class="uinpt" placeholder="配件是否到货">
					<template slot="suffix">
						<u-icon @click="peijian_show=!peijian_show" name="arrow-right"></u-icon>
					</template>
				</u-input>
			</view>
		</view>
		<view class="leimu" style="margin-top: 20rpx;">
			<view class="leimu_title">物品数量</view>
			<view>
				<u-number-box v-model="num" :min="1"></u-number-box>
			</view>
		</view>
		<view class="upimg">
			<view class="upimg_title">上传图片</view>
			<view class="img_box">
				<u-upload uploadText="添加图片" :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1"
					multiple :maxCount="5"></u-upload>
			</view>
			<view class="upimg_tips">(可上传配件图片方便师傅接单服务)</view>
		</view>
		<view class="notes">
			<view class="notes_title">需求备注 <span> (选填)</span></view>
			<u--textarea style="background:rgba(0, 204, 123, 0.05);" v-model="remarks"
				placeholder="请填写物品的详细内容，以便师傅为您服务哦~"></u--textarea>
		</view>
		<view class="btn" @click="confirmDown()">确定下单</view>



		<u-popup :show="leimu_show" mode="bottom" :round="20" @close="close" closeOnClickOverlay>
			<view class="leimu_pop_box" v-if="type!=4">
				<view class="leimu_pop_title">选择物品类目</view>
				<view class="select_leimu_box">
					<view class="left_box">
						<view class="left_item" :class="leimu_index==index?'slitem':''"
							v-for="(item,index) in leimu_list" :key="index" @click="changeLeiMu(item,index)">{{item.name}}
						</view>
					</view>
					<view class="right_box">
						<view class="right_item" v-for="(item,index) in leimu_list[leimu_index].childlist" :key="index"
							@click="confirm(item)">{{item.name}}</view>
					</view>
				</view>
			</view>
			<view v-else class="type4">
				<view class="leimu_pop_title">选择物品类目</view>
				<view class="btnsss">
					<view class="btnss_item" :class="leimu_index1==index?'sel':''" v-for="(item,index) in leimu_list" :key="index" @click="changeFw(item,index)">{{item.name}}</view>
					<!-- <view class="btnss_item" :class="leimu_index1==2?'sel':''" @click="leimu_index1=2">家装类</view>
					<view class="btnss_item" :class="leimu_index1==3?'sel':''" @click="leimu_index1=3">商业店铺</view> -->
				</view>
				<view class="cfm_btn" @click="fwConfirm()">确定</view>
			</view>
		</u-popup>
		<u-popup :show="fuwu_show" mode="bottom" @close="close" closeOnClickOverlay>
			<view class="fuwu_pop_box">
				<view @click="confirmFuWu(1)">安装</view>
				<view @click="confirmFuWu(2)">维修</view>
			</view>
		</u-popup>
		<u-popup :show="peijian_show" mode="bottom" @close="close" closeOnClickOverlay>
			<view class="fuwu_pop_box">
				<view @click="confirmFuWu(3)">配件未到货</view>
				<view @click="confirmFuWu(4)">配件齐全,货已到家</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import store from '@/store/index.js'
	import { mapState } from 'vuex'
	import {category} from "../../api/index.js"
	export default {
		data() {
			return {
				topHeight: '',
				leimu_show: false,
				categoryid_one:'',
				categoryid_two:'',
				serve_type:'',//服务项目
				fittings_status:'',//配件状态
				leimu_index1: 0, //房屋工程
				leimu_index: 0,
				leimu_list: [],
				leimu: '',
				fuwu_show: false,
				fuwu: '',
				peijian: '',
				peijian_show: false,
				num: 1,
				image:[],
				img:'',
				fileList1: [],
				type: 1,
				remarks:'',//备注
				longitude:'',
				latitude:'',
				install_price:'',
				maintain_price:'',
				price:'',
			}
		},
		onLoad(e) {
			this.type=e.type
		},
		onShow() {
			this.getWeiZhi()
			this.getCategory()
		},
		computed:{
			...mapState({
				
			})
		},
		methods: {
			getWeiZhi(){
				uni.getLocation({
					success:res=>{
						this.latitude=res.latitude
						this.longitude=res.longitude
						
					}
				})
			},
			getCategory(){
				if(this.type!=4){
					category({type:'default'}).then(res=>{
						// console.log(res);
						this.leimu_list=res.data
						this.categoryid_one=res.data[0].id
					})
				}else{
					category({type:'project'}).then(res=>{
						console.log(res);
						this.leimu_list=res.data
						this.categoryid_one=res.data[0].id
					})
				}
			},
			close() {
				this.leimu_show = false
				this.fuwu_show = false
				this.peijian_show = false
			},
			changeLeiMu(e,p) { 
				console.log(e);
				this.categoryid_one=e.id
				this.leimu_index = p
				this.pic=e.image
				uni.setStorageSync('pic',e.image)
			},
			confirm(e) {
				console.log(e,999);
				this.maintain_price=e.maintain_price
				this.install_price=e.install_price
				this.categoryid_two=e.id
				this.leimu = this.leimu_list[this.leimu_index].name + '(' + e.name + ')'
				this.leimu_show = false
			},
			confirmFuWu(e) {
				if (e == 1) {
					this.fuwu = '安装'
					this.serve_type=1
					this.price=this.install_price
				} else if (e == 2) {
					this.fuwu = '维修'
					this.serve_type=2
					this.price=this.maintain_price
				} else if (e == 3) {
					this.peijian = '配件未到货'
					this.fittings_status=1
				} else {
					this.peijian = '配件齐全,货已到家'
					this.fittings_status=2
				}
				this.fuwu_show = false
				this.peijian_show = false
			},
			confirmDown() {
				if(!this.categoryid_one||!this.fittings_status||!this.serve_type){
					uni.showToast({
						icon:'error',
						title:'请填写完整信息'
					})
				}else{
					let p ={
						order_type:this.type,
						categoryid_one:this.categoryid_one,
						categoryid_two:this.categoryid_two,
						serve_type:this.serve_type,
						fittings_status:this.fittings_status,
						category_name:this.leimu,
						number:this.num,
						images:this.image,
						pic:this.pic,
						remarks:this.remarks,
						order_type:this.type,
						longitude:this.longitude,
						latitude:this.latitude,
						userprice:this.price
					}
					store.commit('changeData',p)
					uni.navigateTo({
						url: '/pages/index/custom?type=' + this.type
					})
				}
				
			},
			changeFw(a,b){
				this.leimu_index1=b
				this.leimu=a.name
				this.categoryid_one=a.id
				console.log(a,b);
			},
			fwConfirm(){
				this.leimu_show=false
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'https://xiaoan.lf520.vip/api/common/upload',
						filePath: url,
						name: 'file',
						header: {
							token: uni.getStorageSync('tokenVal')
						},
						formData: {
							user: 'test'
						},
						success: (res) => {
							let p=JSON.parse(res.data)
							this.image.push(p.data.fullurl)
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
			back(){
				uni.switchTab({
					url:"/pages/index/index"
				})
			}

		}
	}
</script>

<style scoped lang="scss">
	.oneprice_page {
		width: 100vw;
		min-height: 1000rpx;
		height: auto;
		background-color: #F9F9F9;
		.topbar{
			width: 100%;
			background-color: #00CC7B;
		}
		.page_title{
			width: 100%;
			height: 88rpx;
			line-height: 88rpx;
			text-align: center;
			color: #FFFFFF;
			font-size: 32rpx;
			font-weight: bold;
			background-color: #00CC7B;
			position: relative;
			.back_icon{
				position: absolute;
				top: 20rpx;
				left: 26rpx;
			}
		}

		.leimu {
			width: 100%;
			height: 104rpx;
			display: flex;
			justify-content: space-between;
			padding: 32rpx;
			background-color: #FFFFFF;
			transform: translateY(20rpx);

			.leimu_title {
				font-size: 28rpx;
				font-weight: bold;
				color: #333333;
			}

			.ipt {
				width: auto;
				height: 40rpx;
				border: none;
				background-color: #FFFFFF !important;
			}

			/deep/.uinpt {
				text-align: right;
			}
		}

		.upimg {
			width: 100%;
			height: auto;
			min-height: 350rpx;
			background-color: #FFFFFF;
			padding: 32rpx;
			margin-top: 40rpx;

			.upimg_title {
				font-size: 28rpx;
				color: #333333;
				font-weight: bold;
			}

			.img_box {
				width: 100%;
				height: auto;
				min-height: 160rpx;
				margin-top: 32rpx;
			}

			.upimg_tips {
				font-size: 24rpx;
				color: #999999;
			}
		}

		.notes {
			width: 100%;
			height: 272rpx;
			background-color: #FFFFFF;
			padding: 32rpx;
			margin-top: 20rpx;

			.notes_title {
				font-size: 28rpx;
				font-weight: bold;
				color: #333333;
				margin-bottom: 16rpx;

				span {
					color: #999999;
					font-size: 24rpx;
				}
			}
		}

		.btn {
			width: 686rpx;
			height: 88rpx;
			margin: auto;
			background-color: #00CC7B;
			border-radius: 8rpx;
			color: #FFFFFF;
			text-align: center;
			line-height: 88rpx;
			font-size: 32rpx;
			margin-top: 120rpx;
		}
	}

	.leimu_pop_box {
		width: 100%;
		height: 1094rpx;
		background-color: #FFFFFF;
		border-radius: 20rpx 20rpx 0rpx 0rpx;

		.leimu_pop_title {
			width: 100%;
			font-size: 32rpx;
			color: #333333;
			text-align: center;
			margin-top: 32rpx;
		}

		.select_leimu_box {
			display: flex;
			margin-top: 48rpx;

			.left_item {
				width: 240rpx;
				height: 72rpx;
				line-height: 72rpx;
				font-size: 28rpx;
				color: #666666;
				padding-left: 32rpx;
				background-color: #EEEEEE;
			}

			.slitem {
				color: #00CC7B;
				background-color: #FFFFFF;
			}

			.right_item {
				width: 510rpx;
				background-color: #FFFFFF;
				height: 72rpx;
				line-height: 72rpx;
				font-size: 28rpx;
				padding-left: 32rpx;
				color: #333333;

			}
		}
	}

	.type4 {
		width: 100%;
		height: 438rpx;
		background-color: #FFFFFF;
		border-radius: 20rpx;
		padding: 32rpx;

		.leimu_pop_title {
			width: 100%;
			font-size: 32rpx;
			color: #333333;
			text-align: center;
			margin-top: 32rpx;
		}

		.btnsss {
			width: 100%;
			height: 72rpx;
			margin-top: 64rpx;
			display: flex;

			.btnss_item {
				width: 148rpx;
				height: 72rpx;
				line-height: 72rpx;
				text-align: center;
				border-radius: 8rpx;
				border: 2rpx #DDDDDD solid;
				color: #333333;
				font-size: 28rpx;
				margin-right: 32rpx;
			}

		}

		.cfm_btn {
			width: 100%;
			height: 88rpx;
			line-height: 88rpx;
			text-align: center;
			border-radius: 110rpx;
			background-color: #00CC7B;
			color: #FFFFFF;
			font-size: 32rpx;
			margin-top: 90rpx;
		}
	}

	.fuwu_pop_box {
		width: 100%;
		height: 224rpx;
		background-color: #FFFFFF;
		padding-bottom: 40rpx;
		box-sizing: content-box;

		view {
			width: 100%;
			height: 112rpx;
			line-height: 112rpx;
			text-align: center;
			font-size: 32rpx;
			color: #333333;
		}
	}

	.sel {
		background-color: #00CC7B;
		color: #FFFFFF !important;
		border: none !important;
	}
</style>